package com.medic.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.medic.entity.GroupUser;
import com.medic.entity.VerifyRole;
import com.medic.entity.group.pain.Task;
import com.medic.service.impl.AssitantServiceImpl;
import com.medic.service.impl.DoctorServiceImpl;
import com.medic.service.impl.GroupServiceImpl;
import com.medic.service.impl.PharmacistServiceImpl;
import com.medic.service.impl.SikerinfoServiceImpl;
import com.medic.service.impl.TaskServiceImpl;
import com.medic.service.impl.UserServiceImpl;

@RequestMapping("/web/verify")
@Controller
public class VerifyController {

	@Resource
	private DoctorServiceImpl doctorService;
	@Resource
	private UserServiceImpl userService;
	@Resource
	private AssitantServiceImpl assitantService;
	@Resource
	private PharmacistServiceImpl pharmacistService;

	@Resource
	private GroupServiceImpl groupService;
	@Resource
	private SikerinfoServiceImpl sikerinfoService;
	
	@Resource
	private TaskServiceImpl taskService;

	@RequestMapping("/index")
	public String index() {

		return "/web/VERIFY";
	}

	@RequestMapping("action/{flag}/{role}")
	@ResponseBody
	public Object noaccept(@PathVariable("flag") String flag, @PathVariable("role") String role,
			@RequestParam("ids[]") int[] ids) {
		Map<String, Object> object = new HashMap<String, Object>();

		try {
			switch (flag) {
			case "noaccept":
				switch (role) {
				

				case "assitant":
					groupService.nopassAssitant(ids);
					break;
				case "doctor":
					groupService.nopassDoctor(ids);
					break;
				case "user":
					groupService.nopassUser(ids);
					break;
				case "pharmacist":
					groupService.nopassPharmacist(ids);
					break;
				}
				break;
			case "accept":
				switch (role) {
				case "assitant":
					groupService.checkAssitant(ids);
					break;
				case "doctor":
					groupService.checkDoctor(ids);
					break;
				case "user":

					groupService.checkUser(ids);
					
					// 以下是入组完成后处理的逻辑
					long now = new Date().getTime();
					for (int id : ids) {
						// 这里的 id需要获取
						GroupUser gu = groupService.getGroupUserById(id);
						
						int groupId = gu.getGroupid(), userId = gu.getUserid();
						
						sikerinfoService.initInfo(userId, groupId);

						// 增加入组后的疼痛评价任务
						Task task = new Task();
						task.setCrDate(now);
						task.setGroupId(groupId);
						task.setSikerId(userId);
						// 任务类型为疼痛评价
						task.setTsType(1);
						// 由于批量添加无法获取自增id 所以此处不能使用批量
						taskService.save(task);
						
						// 根据任务id 发送微信消息给患者 让其进行疼痛评价 
						// http://localhost:8080/Client/client/task/doTask?tsId=task.getTsId
						
					}
					break;
				case "pharmacist":
					groupService.checkPharmacist(ids);
					break;
				}
				break;
			}
		} catch (Exception e) {
			
			// 执行失败
			object.put("status", "error");
		}
		// 执行成功
		object.put("status", "ok");

		return object;

	}

	@RequestMapping("/action/detail/{role}/{roleid}")
	public Object detail(@PathVariable("role") String role, @PathVariable("roleid") int roleid, Model model) {

		String result = "";
		try {

			switch (role) {
			case "user":
				model.addAttribute("role", userService.getUser(roleid));
				result = "/web/userinfo";
				break;
			case "doctor":
				model.addAttribute("role", doctorService.getDoctor(roleid));
				result = "/web/doctorinfo";
				break;
			case "assitant":
				model.addAttribute("role", assitantService.getAssitant(roleid));
				result = "/web/doctorinfo";
				break;
			case "pharmacist":
				model.addAttribute("role", pharmacistService.getPharmacist(roleid));
				result = "/web/doctorinfo";
				break;
			}

		} catch (Exception e) {

		}
		return result;
	}

	@RequestMapping("/data/{role}")
	@ResponseBody
	public Object data(@PathVariable("role") String role, @RequestParam("offset") int offset) {
		Map<String, Object> object = new HashMap<String, Object>();

		try {
			if ("EMPTY".equals(role)) {
				object.put("total", 0);
				object.put("rows", new int[] {});
				return object;
			}
			Page<VerifyRole> page = groupService.getNopassRole(role, (offset / 10) + 1);
			object.put("rows", page);
			object.put("total", page.getTotal());

		} catch (Exception e) {
			e.printStackTrace();
		}

		return object;
	}

}
